package com.example.ppjoke.dynamicnav;

import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;

import com.example.ppjoke.AppConfig;

import java.util.HashMap;
import java.util.Iterator;

public class NavigationBuilder {
    public void create(DynamicActivity activity, FragmentManager manager,
                       NavController controller,int contentId){

        NavigatorProvider provider = controller.getNavigatorProvider();
        NavGraph navGraph = new NavGraph(new NavGraphNavigator(provider));
        FixNavigator fixNavigator = new FixNavigator(activity,manager,contentId);
        provider.addNavigator(fixNavigator);
        HashMap<String,DestinationEntity>map = AppConfig.getInstance().destinationMap;
        Iterator<String>it = map.keySet().iterator();//mapkey生成为迭代器
        while (it.hasNext()){
            String key = it.next();
            DestinationEntity entity = map.get(key);
            FragmentNavigator.Destination destination = fixNavigator.createDestination();
            destination.setClassName(entity.className);
            destination.setId(entity.id);
            destination.addDeepLink(entity.pageUrl);
            navGraph.addDestination(destination);
            if (entity.asStarter)
                navGraph.setStartDestination(entity.id);
        }
        controller.setGraph(navGraph);
    }

}
